import { customRequest } from "../request/custom"
import { get, post, del, put, patch } from "../request/request";

/**
 * 获取用户文章列表
 * @param callback 
 */
export const fetchTopicList = async (callback?: () => void | boolean) => {
  return customRequest(() => get("/api/topic"), callback);
};
/**
 * 获取帖子，最早发的在最上面
 * @param callback 
 */
export const fetchEarlyTopicList = async (callback?: () => void | boolean) => {
  return customRequest(() => get("/api/topic/time/asc"), callback);
};
/**
 * 获取帖子，点赞多的在最上面
 * @param callback 
 */
export const fetchHotTopicList = async (callback?: () => void | boolean) => {
  return customRequest(() => get("/api/topic/praise/desc"), callback);
};

/**
 * 获取热门帖子
 * @param callback 
 */
export const fetchHottestTopicList = async (callback?: () => void | boolean) => {
  return customRequest(() => get("/api/topic/sort/hot"), callback);
};


/**
 * 获取指定用户帖子
 * @param id 
 * @param callback 
 */
export const fetchUserTopicList = async (id:string,callback?: () => void | boolean) => {
  return customRequest(
    () => get(`/api/topic/userTopics/${id}`),
    callback
  );
};

/**
 * 添加文章
 * @param formData 
 * @param callback 
 */
export const fetchAddTopic = async (formData: {}, callback?: () => void | boolean) => {
  return customRequest(() => post("/api/topic", formData), callback);
};

/**
 * 获取根据文章id指定帖子
 * @param id 
 * @param callback 
 */
export const fetchTopic = async (id:string, callback?: () => void | boolean) => {
  return customRequest(() => get(`/api/topic/${id}`), callback);
};

/**
 * 删除帖子
 * @param id 
 * @param callback 
 */
export const fetchDelTopic = async (
  id:string,
  callback?: () => void | boolean
) => {
  return customRequest(() => del(`/api/topic/${id}`), callback);
};
/**
 * 编辑帖子
 * @param id 
 * @param formData 
 * @param callback 
 */
export const fetchEditTopic = async (
  id:string,
  formData:any,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => patch(`/api/topic/${id}`, formData),
    callback
  );
};
/**
 * 搜素帖子(keyword)
 * @param keyword 
 * @param callback 
 */
export const fetchSearchTopic = async (
  keyword: string,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => get(`/api/topic?keyword=${keyword}`),
    callback
  );
};
/**
 * 搜素帖子(分类)
 * @param keyword 
 * @param callback 
 */
export const fetchCateTopic = async (
  cate: string,
  callback?: () => void | boolean
) => {
  return customRequest(() => get(`/api/topic?cate=${cate}`), callback);
};



// /////  阅读量  /////
/**
 * 阅读量+1
 * @param id 帖子阅读量
 * @param callback 
 */
export const fetchAddRead = async (id: string, callback?: () => void | boolean) => {
  return customRequest(() => put(`/api/topic/read/${id}`), callback);
};


// /////  收藏  /////
/**
 * 收藏帖子
 * @param id 帖子id
 * @param callback 
 */
export const fetchCollectingTopic = async (
  id: string,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => put(`/api/user/collectingTopic/${id}`),
    callback
  );
};

/**
 * 取消收藏帖子
 * @param id 帖子id
 * @param callback 
 */
export const fetchUnCollectingTopic = async (
  id: string,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => del(`/api/user/uncollectingTopic/${id}`),
    callback
  );
};



// /////  点赞  /////
/**
 * 点赞帖子
 * @param id 帖子id
 * @param callback 
 */
export const fetchLikingTopic = async (
  id: string,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => put(`/api/user/likingTopic/${id}`),
    callback
  );
};

/**
 * 取消点赞帖子
 * @param id 帖子id
 * @param callback 
 */
export const fetchUnLikingTopic = async (
  id: string,
  callback?: () => void | boolean
) => {
  return customRequest(
    () => del(`/api/user/unlikingTopic/${id}`),
    callback
  );
};